package Ch02.Shili.Sl1;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class Demo  {
    public static void main(String[] args) throws Exception{
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory bdf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db = bdf.newDocumentBuilder();
        //解析XML文档，得到一个Document对象，即DOM树
        org.w3c.dom.Document doc =  db.parse("src/Ch02/Shili/Sl1/box.xml");
        //得到所有Brand节点列表的信息
        NodeList brandList = doc.getElementsByTagName("Brand");
        //循环Brand信息
        for (int i=0;i<brandList.getLength();i++){
            //获取第i个Brand元素信息
            Node brand=brandList.item(i);
            //获取第i个Brand元素的name属性的值
            Element element=(Element) brand;
            String attrValue=element.getAttribute("name");
            //获取第i个Brand元素的所有子元素的name属性值
            NodeList types = element.getChildNodes();
            for (int j=0;j< types.getLength();i++){
                Element typeElement=((Element) types.item(j));
                String type = typeElement.getAttribute("name");
                System.out.println("手机: "+attrValue+type);
            }
        }

    }
}
